<?php
/**
 * Created by PhpStorm.
 * User: juplus-06
 * Date: 2017/9/14
 * Time: 10:36
 */

namespace app\admin\controller;
use think\Controller;



class Mail extends Controller
{

    public function email($email) {

        // return 3212321654;
        // return $email;
        if(!empty($email)){
            $toemail=$email;
            $name='VDMA';
            $subject='测试';
            $content="测试";
            $return  = $this->send_mail($toemail,$name,$subject,$content);
            dump($return);
        }


    }
    function test(){
        $this->email('745598012@qq.com');
    }


    /**
     * 发送邮件方法
     * @param string $to：接收者邮箱地址
     * @param string $title：邮件的标题
     * @param string $content：邮件内容
     * @return boolean  true:发送成功 false:发送失败
     */
    function send_mail($tomail, $name, $subject = '', $body = '', $attachment = null){

        //实例化PHPMailer核心类
        vendor('phpmailer.phpmailer.src');
        $mail = new \PHPMailer\PHPMailer\PHPMailer();


        $mail->CharSet = 'UTF-8';           //设定邮件编码，默认ISO-8859-1，如果发中文此项必须设置，否则乱码
        $mail->IsSMTP();                    // 设定使用SMTP服务
        $mail->SMTPDebug = 0;               // SMTP调试功能 0=关闭 1 = 错误和消息 2 = 消息
        $mail->SMTPAuth = true;             // 启用 SMTP 验证功能
        $mail->SMTPSecure = 'ssl';          // 使用安全协议
        $mail->Host = "smtp.aliyun.com"; // SMTP 服务器
        $mail->Port = 465;                  // SMTP服务器的端口号
        $mail->Username = "yche@keji6.cn";    // SMTP服务器用户名
        $mail->Password = "HEzi199192";     // SMTP服务器密码
        $mail->SetFrom('yche@keji6.cn', 'kejiniu');
        $replyEmail = '';                   //留空则为发件人EMAIL
        $replyName = '';                    //回复名称（留空则为发件人名称）
        $mail->AddReplyTo($replyEmail, $replyName);
        $mail->Subject = $subject;
        $mail->IsHTML(true);
        $mail->MsgHTML($body);
        $mail->AddAddress($tomail, $name);
        if (is_array($attachment)) { // 添加附件
            foreach ($attachment as $file) {
                is_file($file) && $mail->AddAttachment($file);
            }
        }
        return $mail->Send() ? true : $mail->ErrorInfo;
    }


    public function email_yz(){
        //当前时间和发送验证码时间做比较
        $param = input('param.');
        // dump($param['v']);exit;
        $data = Db('email_yz')->where('uid',$param['k'])->find();
        if(!empty($data)){
            $time = round(((time()-$data['time'])/86400));//天数
            // $time = round((((time()-$data['time'])%86400)/3600));//小时
            // $time = (((time()-$data['time'])%86400)%3600)/60;//分钟
            // dump($time);exit;
            if($time>7){
                //超过有效时限7天
                $email = Db('user')->where('id',$param['k'])->value('email');
                $toemail=$email;
                $name='VDMA';
                $subject='账号验证激活';
                $content="您VDMA账号已超过验证有效期,请联系管理员!";
                $this->send_mail($toemail,$name,$subject,$content);
                $audit = Db('user')->where('id',$param['k'])->value('audit');
                if($audit !== 0){
                    Db('user')->where('id',$param['k'])->update(['audit'=>0]);
                }
                echo '您VDMA账号已超过验证有效期,请联系管理员!';
            }else{
                if($data['yzm'] == $param['v']){
                    Db('user')->where('id',$param['k'])->update(['audit'=>1]);
                    $yz_status = Db('email_yz')->where('uid',$param['k'])->value('yz_status');
                    if($yz_status !== 1){
                        Db('email_yz')->where('uid',$param['k'])->update(['yz_status'=>1]);
                    }

                }
                echo '激活成功!';
            }
        }

    }


    //七天后没有验证邮箱的账号全部封号
    public function yz_day(){
        $data = Db('email_yz')->select();
        foreach ($data as $k => $v) {
            // dump($v['uid']);
            if($v['yz_status'] == 0){
                $audit = Db('user')->where('id',$v['uid'])->value('audit');
                if($audit !== 0){
                   $test = Db('user')->where('id',$v['uid'])->update(['audit'=>0]);
                   if($test>0){
                    $email = Db('user')->where('id',$v['uid'])->value('email');
                    $toemail=$email;
                    $name='VDMA';
                    $subject='账号验证激活';
                    $content="您已超过验证有效期,请联系管理员!";
                    $this->send_mail($toemail,$name,$subject,$content);
                   }

                }
            }
        }

    }













}